<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
        <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password"/>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                注册
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
import request from '@/request/request'
import { ElMessage } from 'element-plus'
import router from '@/router'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: ''
})
const rules = reactive({
    username: [
        { required: true, message: 'Please input Activity name', trigger: 'blur' },
        { min: 5, max: 11, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    password: [
        { pattern:/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{6,16}$/, message: 'Please input Activity name', trigger: 'blur' },
        // { min: 5, max: 10, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
})
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
            request.post('/register', ruleForm).then(res => {
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '注册成功',
                        type: 'success',
                    })
                    router.push('/login')
                } else {
                    ElMessage.error('注册失败')
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>

<style lang="scss" scoped>
.el-form{
    width: 300px;height: 150px;border: 1px solid #000;
    padding: 40px 10px;
    float: right;
    margin-top: 40%;
    .el-button{
        margin-left: 40px;
        margin-top: 10px;
    }
}
</style>